<template>
  <div class="app-container">
    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button type="success" plain icon="el-icon-edit" size="mini" :disabled="single" @click="handleUpdate"
          v-hasPermi="['meter:warningConfig:edit']">配置</el-button>
      </el-col>
      <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>

    <el-table v-loading="loading" :data="warningConfigList" @selection-change="handleSelectionChange">
      <el-table-column type="selection" width="55" align="center" />
      <el-table-column label="主键" align="center" prop="configId" />
      <el-table-column label="配置类型" align="center" prop="configType">
        <template slot-scope="scope">
          <dict-tag :options="dict.type.md_assets_category" :value="scope.row.configType" />
        </template>
      </el-table-column>
      <el-table-column label="下限" align="center" prop="lowerLimit" />
      <el-table-column label="上限" align="center" prop="upperLimit" />
      <el-table-column label="是否开启报警" align="center" prop="isOpen">
        <template slot-scope="scope">
          <dict-tag :options="dict.type.ac_is_open" :value="scope.row.isOpen" />
        </template>
      </el-table-column>
      <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
        <template slot-scope="scope">
          <el-button size="mini" type="text" icon="el-icon-edit" @click="handleUpdate(scope.row)"
            v-hasPermi="['meter:warningConfig:edit']">配置</el-button>
        </template>
      </el-table-column>
    </el-table>

    <pagination v-show="total > 0" :total="total" :page.sync="queryParams.pageNum" :limit.sync="queryParams.pageSize"
      @pagination="getList" />

    <!-- 添加或修改报警配置对话框 -->
    <el-dialog :title="title" :visible.sync="open" width="500px" append-to-body>
      <el-form ref="form" :model="form" :rules="rules" label-width="110px">
        <el-form-item label="下限" prop="lowerLimit">
          <el-input v-model.number="form.lowerLimit" placeholder="请输入下限" maxlength="11" />
        </el-form-item>
        <el-form-item label="上限" prop="upperLimit">
          <el-input v-model.number="form.upperLimit" placeholder="请输入上限" maxlength="11" />
        </el-form-item>
        <el-form-item label="是否开启报警" prop="isOpen">
          <el-select v-model="form.isOpen" placeholder="请选择是否开启报警器" clearable>
            <el-option v-for="dict in dict.type.ac_is_open" :key="dict.value" :label="dict.label"
              :value="dict.value"></el-option>
          </el-select>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import { listWarningConfig, getWarningConfig, delWarningConfig, addWarningConfig, updateWarningConfig } from "@/api/meter/warningConfig";

export default {
  name: "WarningConfig",
  dicts: ["md_assets_category", "ac_is_open"],
  data() {
    return {
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 报警配置表格数据
      warningConfigList: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        configType: null,
        lowerLimit: null,
        upperLimit: null,
        isOpen: null
      },
      // 表单参数
      form: {},
      // 表单校验
      rules: {
        lowerLimit: [{
          required: true,
          message: '下限不能为空'
        },
        {
          type: 'number',
          message: '上限必须为数字值'
        }],
        upperLimit: [{
          required: true,
          message: '上限不能为空'
        },
        {
          type: 'number',
          message: '上限必须为数字值'
        }],
        isOpen: [{
          required: true,
          message: '是否开启报警不能为空',
          trigger: 'change'
        }]
      }
    };
  },
  created() {
    this.getList();
  },
  methods: {
    /** 查询报警配置列表 */
    getList() {
      this.loading = true;
      listWarningConfig(this.queryParams).then(response => {
        this.warningConfigList = response.rows;
        this.total = response.total;
        this.loading = false;
      });
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {
        configId: null,
        configType: null,
        lowerLimit: null,
        upperLimit: null,
        isOpen: null
      };
      this.resetForm("form");
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.handleQuery();
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map(item => item.configId)
      this.single = selection.length !== 1
      this.multiple = !selection.length
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.reset();
      const configId = row.configId || this.ids
      getWarningConfig(configId).then(response => {
        this.form = response.data;
        this.open = true;
        this.title = "修改报警配置";
      });
    },
    /** 提交按钮 */
    submitForm() {
      this.$refs["form"].validate(valid => {
        if (valid) {
          if (this.form.configId != null) {
            updateWarningConfig(this.form).then(response => {
              this.$modal.msgSuccess("修改成功");
              this.open = false;
              this.getList();
            });
          } else {
            addWarningConfig(this.form).then(response => {
              this.$modal.msgSuccess("新增成功");
              this.open = false;
              this.getList();
            });
          }
        }
      });
    },
  }
};
</script>
